//
//  UILabel+Category.swift
//  LoveYC
//  UILabel扩展方法
//  Created by Red App on 6/14/19.
//  Copyright © 2019 China Electronic Intelligence System Technology Co., Ltd. All rights reserved.
//

import Foundation

public let kGlobleFontFamilyName = ".SFUIText-Medium"

public var kGloble_smarll_font = 14.0
public var kGloble_normal_font = 15.0

/// 平法字体
enum PingFangFont: String {
    case Medium = ".SFUIText-Medium"
    case Semibold = ".SFUIText-Semibold"
//    case Regular = ".SFUIText-Regular"
}

extension UILabel {
//    public override convenience init(frame: CGRect) {
//
//    }
    
    open override func awakeFromNib() {
//        super.awakeFromNib()
//        setGloableFont()
//        ChangeMethod()
    }
    
//    public override init(frame: CGRect) {
//
//        ChangeMethod()
//    }
    
    /// 设置labe的字体和大型
    /// - Parameter name: 字体
    /// - Parameter size: 大小
    func setLableNameAndSize(_ name: PingFangFont, _ size: CGFloat) {
        self.font = UIFont.pingfangSC(name, size)
    }
    
    
    /// 设置全局字体
    func setGloableFont() {
        self.font = UIFont(name: kGlobleFontFamilyName, size: CGFloat(17))
    }
    
    /// 设置全局字体
    /// - Parameter name: 字体
    /// - Parameter size: 大小
    open func setGlobleFontWith(name: String = kGlobleFontFamilyName, size: CGFloat) {
        self.font = UIFont(name: name, size: size + getSetFontSize())
    }
    
    
    /// 获取设置字体大小
    open func getSetFontSize() -> CGFloat {
        guard let n = UserDefaults.standard.object(forKey: "set_font_size") else {
            return 0.0
        }
        return n as! CGFloat
    }
    
    /// 设置默认字体
    /// - Parameter step: 默认字体步长
    open class func setDefaultFontSize(step: CGFloat) {
        UserDefaults.standard.set(step, forKey: "set_font_size")
    }
    
//    public class func initializeMethod() {
//        ChangeMethod()
//    }
    
    /// method修改方法调用 runtime模型应用
    func ChangeMethod() -> Void {
        
        // 获取交换之前的方法
        let originaMethodC = class_getInstanceMethod(object_getClass(self), #selector(awakeFromNib))
        // 获取交换之后的方法
        let swizzeMethodC  = class_getInstanceMethod(object_getClass(self), #selector(self.swizzeMethod))
        
        //替换类中已有方法的实现,如果该方法不存在添加该方法
        //获取方法的Type字符串(包含参数类型和返回值类型)
        //class_replaceMethod(object_getClass(self), #selector(self.swizzeMethod), method_getImplementation(originaMethodC), method_getTypeEncoding(originaMethodC))
        
        print("你交换两个方法的实现")
        method_exchangeImplementations(originaMethodC!, swizzeMethodC!)
    }
    
    @objc dynamic func originaMethod() -> Void {
        
        print("我是交换之前的方法")
    }
    
    @objc dynamic func swizzeMethod() -> Void {
        self.swizzeMethod()
        self.font = UIFont.systemFont(ofSize: 20)
        print("我是交换之后的方法")
    }
    
   class  func fastCreatLabel(text : String,_ font : UIFont? = UIFont.reFontSystemFontSize(14),_ textcolor : UIColor? = UIColor.hexColor(hexString: "#252525")) -> UILabel {
        let label = UILabel()
        label.text = text
        label.font = font
        label.textColor = textcolor
        return label
    }
    
}
